﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormDeliver
    Dim objCA As New clsLXCK
    Dim objProdInfo As New clsProdInfo
    Dim dtCk As New DataTable
    Dim ids As String
    Dim mes_ids As String
    Dim prod_ids As String
    Dim type_values As String
    Dim httpHelper As New HttpHelper
    Public usr As String
    Dim warehousecode As String
    Private Sub FormDeliver_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        initDt()
        txtProdId.Focus()
    End Sub
    Public Sub initDt()
        dtCk.Columns.Add("sys_id")
        dtCk.Columns.Add("location")
        dtCk.Columns.Add("area")
        dtCk.Columns.Add("prod_id")
        dtCk.Columns.Add("b_pgd_mes_sys_id")
        dtCk.Columns.Add("into_date")
        dtCk.Columns.Add("into_ope")
        dtCk.Columns.Add("type_value")
    End Sub
    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                Dim dt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "0")
                If dt.Rows.Count = 1 Then
                    Dim dr As DataRow() = dtCk.Select("sys_id='" & dt.Rows(0)("sys_id") & "'")
                    If dr.Length = 0 Then
                        dtCk.Rows.Add(dt.Rows(0)("sys_id"), dt.Rows(0)("location"), dt.Rows(0)("area"), dt.Rows(0)("prod_id"),
                                        dt.Rows(0)("b_pgd_mes_sys_id"), dt.Rows(0)("into_date"), dt.Rows(0)("into_ope"), dt.Rows(0)("type_value"))
                        ids &= "'" & dt.Rows(0)("sys_id") & "',"
                        mes_ids &= "'" & dt.Rows(0)("b_pgd_mes_sys_id") & "',"
                        prod_ids &= "'" & dt.Rows(0)("prod_id") & "',"
                        type_values &= "'" & dt.Rows(0)("type_value") & "',"
                        dgv.AutoGenerateColumns = False
                        dgv.DataSource = dtCk
                        lblRowCount.Text = dgv.Rows.Count
                        txtProdId.Text = String.Empty
                        txtProdId.Focus()
                    Else
                        MsgBox("该标签号已在表格中存在！", vbYes, "提示")
                    End If
                    Dim dtt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim, "12") '还需增加查询条件

                Else
                    If dt.Rows.Count = 0 Then
                        MsgBox("未查询到此生产编号库存信息", vbYes, "提示")
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub btnOUT_Click(sender As Object, e As EventArgs) Handles btnOUT.Click
        If String.IsNullOrEmpty(usr) = False Then
            If dtCk.Rows.Count > 0 Then
                If MsgBox("确认出库吗？", vbYesNo, "提示") = vbYes Then
                    If ids.Length > 0 Then
                        ids = ids.Substring(0, ids.Length - 1)
                    End If
                    If mes_ids.Length > 0 Then
                        mes_ids = mes_ids.Substring(0, mes_ids.Length - 1)
                    End If
                    Dim mes_id_list As String() = mes_ids.Split(",")
                    If prod_ids.Length > 0 Then
                        prod_ids = prod_ids.Substring(0, prod_ids.Length - 1)
                    End If
                    Dim prod_id_list As String() = prod_ids.Split(",")
                    If objCA.outCKs(ids, prod_ids, usr) = -2 Then
                        MsgBox("出库失败", vbYes, "提示")
                    Else
                        If type_values.Length > 0 Then
                            type_values = type_values.Substring(0, type_values.Length - 1)
                        End If
                        Dim ty_v_list As String() = type_values.Split(",")
                        Dim ok_mes_ck As Integer = 0
                        For index = 0 To ty_v_list.Count() - 1
                            Dim rnt_mes_ck As String = mesCk("1", warehousecode, ty_v_list(index))
                            Dim jo As JObject = JsonConvert.DeserializeObject(rnt_mes_ck)
                            Dim code As String = jo("code").ToString() '输出0
                            Dim msg As String = jo("msg").ToString() '输出操作成功
                            objCA.update_OutCK_MES(prod_id_list(index), "1", code)
                            If code.Equals("0") Then
                                ok_mes_ck = ok_mes_ck + 1
                            End If
                        Next
                        If ty_v_list.Count() = ok_mes_ck Then
                            MsgBox("出库成功", vbYes, "提示")
                        Else
                            MsgBox("出库成功，mes出库成功数：" & ok_mes_ck, vbYes, "提示")
                        End If
                        dtCk.Rows.Clear()
                        dgv.Refresh()
                        lblRowCount.Text = dgv.Rows.Count
                        ids = String.Empty
                        mes_ids = String.Empty
                        prod_ids = String.Empty
                        type_values = String.Empty
                        txtProdId.Focus()
                    End If
                End If
            Else
                MsgBox("请扫描或输入标签号以进行出库", vbYes, "提示")
            End If
        Else
            MsgBox("请选择用户进行登录", vbYes, "提示")
        End If
    End Sub

    Private Function mesCk(oper_num As String, warehousecode As String, productorname As String) As String
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """xhcount"":""" & Double.Parse(oper_num) & """," '出库数量
        params &= """warehousecode"":""" & warehousecode & """," '出库仓库编码
        params &= """productorname"":""" & productorname & """" '产品编码
        params &= "}"
        Return httpHelper.HttpPostWebService("materialsConsumption", params)
    End Function

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        If dgv.SelectedRows.Count > 0 Then
            dtCk.Rows.RemoveAt(dgv.CurrentRow.Index)
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtCk
            lblRowCount.Text = dgv.Rows.Count
        End If
    End Sub

    Private Sub dgv_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgv.RowPostPaint
        Dim Rectangle As Rectangle = New Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height)
        TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, Rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter Or TextFormatFlags.Right)
    End Sub
End Class